<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>05-CSS样式表的继承性和层叠性</title>
  
    
      <meta 
        property="og:title" 
        content="05-CSS样式表的继承性和层叠性">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/05-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7%E5%92%8C%E5%B1%82%E5%8F%A0%E6%80%A7/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2002-01-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E9%87%8D%E7%82%B9"><span class="toc-text">本文重点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7"><span class="toc-text">CSS的继承性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E5%B1%82%E5%8F%A0%E6%80%A7"><span class="toc-text">CSS的层叠性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">层叠性：计算权重</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%E4%B8%BE%E4%BE%8B"><span class="toc-text">层叠性举例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">举例1：计算权重</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%9D%83%E9%87%8D%E7%9B%B8%E5%90%8C%E6%97%B6"><span class="toc-text">举例2：权重相同时</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B3%EF%BC%9A%E5%85%B7%E6%9C%89%E5%AE%9E%E6%88%98%E6%80%A7%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="toc-text">举例3：具有实战性的例子</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B4%EF%BC%9A%E7%BB%A7%E6%89%BF%E6%80%A7%E9%80%A0%E6%88%90%E7%9A%84%E5%BD%B1%E5%93%8D"><span class="toc-text">举例4：继承性造成的影响</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E6%9D%83%E9%87%8D%E8%AE%A1%E7%AE%97%E7%9A%84%E9%97%AE%E9%A2%98%E5%A4%A7%E6%80%BB%E7%BB%93%EF%BC%88%E9%9D%9E%E5%B8%B8%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">层叠性：权重计算的问题大总结（非常重要）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E5%86%B2%E7%AA%81%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">CSS样式表的冲突的总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%E6%BC%94%E7%A4%BA"><span class="toc-text">题目演示</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE1"><span class="toc-text">题目1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE2"><span class="toc-text">题目2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE3"><span class="toc-text">题目3</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE4"><span class="toc-text">题目4</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%83%E9%87%8D%E9%97%AE%E9%A2%98%E6%B7%B1%E5%85%A5"><span class="toc-text">权重问题深入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E4%B8%80%E4%B8%AA%E6%A0%87%E7%AD%BE%EF%BC%8C%E6%90%BA%E5%B8%A6%E4%BA%86%E5%A4%9A%E4%B8%AA%E7%B1%BB%E5%90%8D%EF%BC%8C%E6%9C%89%E5%86%B2%E7%AA%81%EF%BC%9A"><span class="toc-text">同一个标签，携带了多个类名，有冲突：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#important%E6%A0%87%E8%AE%B0%EF%BC%9A%E4%BC%98%E5%85%88%E7%BA%A7%E6%9C%80%E9%AB%98"><span class="toc-text">!important标记：优先级最高</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE"><span class="toc-text">知识回顾</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#class%E5%92%8Cid%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">class和id的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%84%E7%A7%8D%E9%80%89%E6%8B%A9%E5%99%A8-%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">各种选择器(浏览器兼容性)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E4%B8%A4%E4%B8%AA%E6%80%A7%E8%B4%A8"><span class="toc-text">css两个性质</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      05-CSS样式表的继承性和层叠性
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2001-12-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2002-01-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          CSS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>2.8k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            CSS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="本文重点"><a href="#本文重点" class="headerlink" title="本文重点"></a>本文重点</h2><ul>
<li>CSS的继承性</li>
<li>CSS的层叠性<ul>
<li>计算权重</li>
<li>权重问题大总结</li>
<li>CSS样式表的冲突的总结</li>
</ul>
</li>
<li>权重问题深入<ul>
<li>同一个标签，携带了多个类名</li>
<li>!important标记</li>
</ul>
</li>
</ul>
<h2 id="CSS的继承性"><a href="#CSS的继承性" class="headerlink" title="CSS的继承性"></a>CSS的继承性</h2><p>我们来看下面这样的代码，来引入继承性：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170724_2359.png" class="lozad post-image"src="http://img.smyhvae.com/20170724_2359.png"></p>
<p>上方代码中，我们给div标签增加红色属性，却发现，div里的每一个子标签<code>&lt;p&gt;</code>也增加了红色属性。于是我们得到这样的结论：</p>
<blockquote>
<p>有一些属性，当给自己设置的时候，自己的后代都继承上了，这个就是<strong>继承性。</strong></p>
</blockquote>
<p>继承性是从自己开始，直到最小的元素。</p>
<p>但是呢，如果再给上方的代码加一条属性：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170725_2122.jpg" class="lozad post-image"src="http://img.smyhvae.com/20170725_2122.jpg"></p>
<p>上图中，我们给div加了一个border，但是发现只有div具备了border属性，而p标签却没有border属性。于是我们可以得出结论：</p>
<ul>
<li><p>关于文字样式的属性，都具有继承性。这些属性包括：color、 text-开头的、line-开头的、font-开头的。</p>
</li>
<li><p>关于盒子、定位、布局的属性，都不能继承。</p>
</li>
</ul>
<p>以后当我们谈到css有哪些特性的时候，我们要首先想到继承性。而且，要知道哪些属性具有继承性、哪些属性没有继承性。</p>
<h2 id="CSS的层叠性"><a href="#CSS的层叠性" class="headerlink" title="CSS的层叠性"></a>CSS的层叠性</h2><p>很多公司如果要笔试，那么一定会考层叠性。</p>
<h3 id="层叠性：计算权重"><a href="#层叠性：计算权重" class="headerlink" title="层叠性：计算权重"></a>层叠性：计算权重</h3><p><strong>层叠性：就是css处理冲突的能力。</strong> 所有的权重计算，没有任何兼容问题！</p>
<p>CSS像艺术家一样优雅，像工程师一样严谨。</p>
<p>我们来看一个例子，就知道什么叫层叠性了。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170725_2132.png" class="lozad post-image"src="http://img.smyhvae.com/20170725_2132.png"></p>
<p>上图中，三种选择器同时给P标签增加颜色的属性，但是，文字最终显示的是蓝色，这个时候，就出现了层叠性的情况。</p>
<p>当多个选择器，选择上了某个元素的时候，要按照如下顺序统计权重：</p>
<ul>
<li>id 选择器</li>
<li>类选择器、属性选择器、伪类选择器</li>
<li>标签选择器、伪元素选择器</li>
</ul>
<p>因为对于相同方式的样式表，其选择器排序的优先级为：ID选择器 &gt; 类选择器 &gt; 标签选择器</p>
<p>针对上面这句话，我们接下来举一些复杂一点的例子。</p>
<h3 id="层叠性举例"><a href="#层叠性举例" class="headerlink" title="层叠性举例"></a>层叠性举例</h3><h4 id="举例1：计算权重"><a href="#举例1：计算权重" class="headerlink" title="举例1：计算权重"></a>举例1：计算权重</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170725_2138.png" class="lozad post-image"src="http://img.smyhvae.com/20170725_2138.png"></p>
<p>如上图所示，统计各个选择器的数量，优先级高的胜出。文字的颜色为红色。</p>
<p>PS：不进位，实际上能进位（奇淫知识点：255个标签，等于1个类名）但是没有实战意义！</p>
<h4 id="举例2：权重相同时"><a href="#举例2：权重相同时" class="headerlink" title="举例2：权重相同时"></a>举例2：权重相同时</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170725_2250.png" class="lozad post-image"src="http://img.smyhvae.com/20170725_2250.png"></p>
<p>上图可以看到，第一个样式和第二个样式的权重相同。但第二个样式的书写顺序靠后，因此以第二个样式为准（就近原则）。</p>
<h4 id="举例3：具有实战性的例子"><a href="#举例3：具有实战性的例子" class="headerlink" title="举例3：具有实战性的例子"></a>举例3：具有实战性的例子</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170726_2221.png" class="lozad post-image"src="http://img.smyhvae.com/20170726_2221.png"></p>
<p>现在我要让一个列表实现上面的这种样式：第一个li为红色，剩下的li全部为蓝色。</p>
<p>如果写成下面这种代码是无法实现的：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170726_2225.png" class="lozad post-image"src="http://img.smyhvae.com/20170726_2225.png"></p>
<p>无法实现的原因很简单，计算一下三个选择器的权重就清楚了，显然第二个样式被第一个样式表覆盖了。</p>
<p>正确的做法是：（<strong>非常重要</strong>）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170726_2229.png" class="lozad post-image"src="http://img.smyhvae.com/20170726_2229.png"></p>
<p>上图中，第二个样式比第一个样式的权重要大。因此在实战中可以实现这种效果：<strong>所有人当中，让某一个人为红，让其他所有人为蓝。</strong></p>
<p>这种方式好用是好用，但用好很难。</p>
<p>就拿上方代码来举例，为了达到这种效果，即为了防止权重不够，比较稳妥的做法是：<strong>把第二个样式表照着第一个样式表来写，在此基础上，给第二个样式表再加一个权重。</strong></p>
<p>上面这个例子很具有实战性。</p>
<h4 id="举例4：继承性造成的影响"><a href="#举例4：继承性造成的影响" class="headerlink" title="举例4：继承性造成的影响"></a>举例4：继承性造成的影响</h4><p>这里需要声明一点：</p>
<blockquote>
<p>如果不能直接选中某个元素，通过继承性影响的话，那么权重是0。</p>
</blockquote>
<p>为了验证上面这句话，我们来看看下面这样的例子：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0843.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0843.png"></p>
<p>另外：<strong>如果大家的权重相同，那么就采用就近原则：谁描述的近，听谁的</strong>。举例如下：(box3 描述得最近，所以采用 box3 的属性)</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190122_1530.png" class="lozad post-image"src="http://img.smyhvae.com/20190122_1530.png"></p>
<p>上方代码的文字版如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">      <span class="selector-id">#box1</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">      &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">      <span class="selector-id">#box2</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: green;</span></span><br><span class="line"><span class="language-css">      &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">      <span class="selector-id">#box3</span> &#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">color</span>: blue;</span></span><br><span class="line"><span class="language-css">      &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box1&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box2&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box3&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">p</span>&gt;</span>猜猜我是什么颜色<span class="tag">&lt;/<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>

<h3 id="层叠性：权重计算的问题大总结（非常重要）"><a href="#层叠性：权重计算的问题大总结（非常重要）" class="headerlink" title="层叠性：权重计算的问题大总结（非常重要）"></a>层叠性：权重计算的问题大总结（非常重要）</h3><p>层叠性。层叠性是一种能力，就是处理冲突的能力。当不同选择器，对一个标签的同一个样式，有不同的值，听谁的？这就是冲突。css有着严格的处理冲突的机制。</p>
<p>通过列举上面几个例子，我们对权重问题做一个总结。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2050.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2050.png"></p>
<p>上面这个图非常重要，我们针对这个图做一个文字描述：</p>
<ul>
<li>选择上了，数权重，(id的数量，类的数量，标签的数量)。如果权重一样，谁写在后面听谁的。</li>
<li>没有选择上，通过继承影响的，就近原则，谁描述的近听谁的。如果描述的一样近，比如选择器权重，如果权重再一样重，谁写在后面听谁的。</li>
</ul>
<h3 id="CSS样式表的冲突的总结"><a href="#CSS样式表的冲突的总结" class="headerlink" title="CSS样式表的冲突的总结"></a>CSS样式表的冲突的总结</h3><ul>
<li>1、对于相同的选择器（比如同样都是类选择器），其样式表排序：行级样式 &gt; 内嵌样式表 &gt; 外部样式表（就近原则）</li>
<li>2、对于相同类型的样式表（比如同样都是内部样式表），其选择器排序：ID选择器 &gt; 类选择器 &gt; 标签选择器</li>
<li>3、外部样式表的ID选择器  &gt; 内嵌样式表的标签选择器</li>
</ul>
<blockquote>
<p>总结：就近原则。ID选择器优先级最大。</p>
</blockquote>
<p>举例：如果都是内嵌样式表，优先级的顺序如下：（ID 选择器 &gt; 类选择器 &gt; 标签选择器）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-14.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-14.png"></p>
<p>另外还有两个冲突的情况：</p>
<ul>
<li>1、对同一个标签，如果用到的都是内嵌样式表，且权重一致，那它的优先级：<strong>定义</strong>的CSS样式表中，谁最近，就用谁。</li>
<li>2、对于同一个标签，如果用到的都是外部样式表，且权重一致，那它的优先级：html文件中，引用样式表的位置越近，就用谁。</li>
</ul>
<p>例如：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/2015-10-03-css-16.png" class="lozad post-image"src="http://img.smyhvae.com/2015-10-03-css-16.png"></p>
<h3 id="题目演示"><a href="#题目演示" class="headerlink" title="题目演示"></a>题目演示</h3><p>CSS的层叠性讲完了，我们来做几个题目吧。</p>
<h4 id="题目1"><a href="#题目1" class="headerlink" title="题目1"></a>题目1</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0851.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0851.png"></p>
<h4 id="题目2"><a href="#题目2" class="headerlink" title="题目2"></a>题目2</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0853.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0853.png"></p>
<h4 id="题目3"><a href="#题目3" class="headerlink" title="题目3"></a>题目3</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0855.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0855.png"></p>
<h4 id="题目4"><a href="#题目4" class="headerlink" title="题目4"></a>题目4</h4><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0857.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0857.png"></p>
<h2 id="权重问题深入"><a href="#权重问题深入" class="headerlink" title="权重问题深入"></a>权重问题深入</h2><h3 id="同一个标签，携带了多个类名，有冲突："><a href="#同一个标签，携带了多个类名，有冲突：" class="headerlink" title="同一个标签，携带了多个类名，有冲突："></a>同一个标签，携带了多个类名，有冲突：</h3><p>这里需要补充两种冲突的情况：</p>
<ul>
<li>1、对同一个标签，如果用到了了多个相同的内嵌样式表，它的优先级：<strong>定义</strong>的样式表中，谁最近，就用谁。</li>
<li>2、对于同一个标签，如果引用了多个相同的外部样式表，它的优先级：html文件中，引用样式表的位置越近，就用谁。</li>
</ul>
<p>例如：（就近原则）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2021.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2021.png"></p>
<p>上图中，<strong>文字显示的颜色均为红色</strong>。因为这和在标签中的挂类名的书序无关，只和css的顺序有关。</p>
<h3 id="important标记：优先级最高"><a href="#important标记：优先级最高" class="headerlink" title="!important标记：优先级最高"></a>!important标记：优先级最高</h3><p>来看个很简单的例子：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2029.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2029.png"></p>
<p>上图中，显然id选择器的权重最大，所以文字的颜色是红色。</p>
<p>如果我们想让文字的颜色显示为绿色，只需要给标签选择器的加一个<code>!important</code>标记，此时其权重为无穷大。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2035_2.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2035_2.png"></p>
<p>important是英语里面的“重要的”的意思。我们可以通过如下语法：</p>
<pre class="highlight"><span class="line">k:v !important;</span><br></pre>

<p>来给一个属性提高权重。这个属性的权重就是<strong>无穷大</strong>。</p>
<p>注意，一定要注意语法的正确性。</p>
<p>正确的语法：</p>
<pre class="highlight"><span class="line">font-size:60px !important;</span><br></pre>

<p>错误的语法：</p>
<pre class="highlight"><span class="line">font-size:60px; !important;    不能把!important写在外面</span><br><span class="line">font-size:60px important;      不能忘记感叹号</span><br></pre>

<p><code>!important</code>标记需要强调如下3点：</p>
<p><strong>（1）!important提升的是一个属性，而不是一个选择器</strong></p>
<pre class="highlight"><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	<span class="attribute">color</span>:red <span class="meta">!important</span>;    只写了这一个!important，所以只有字体颜色属性提升了权重</span><br><span class="line">	<span class="attribute">font-size</span>: <span class="number">100px</span> ;       这条属性没有写!important，所以没有提升权重</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-id">#para1</span>&#123;</span><br><span class="line">	<span class="attribute">color</span>:blue;</span><br><span class="line">	<span class="attribute">font-size</span>: <span class="number">50px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.spec</span>&#123;</span><br><span class="line">	<span class="attribute">color</span>:green;</span><br><span class="line">	<span class="attribute">font-size</span>: <span class="number">20px</span>;</span><br><span class="line">&#125;</span><br></pre>

<p>所以，综合来看，字体颜色是red（听important的）；字号是50px（听id的）。</p>
<p><strong>（2）!important无法提升继承的权重，该是0还是0</strong></p>
<p>比如HTML结构：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span>哈哈哈哈哈哈哈哈<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>有CSS样式：</p>
<pre class="highlight"><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">color</span>:red <span class="meta">!important</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	<span class="attribute">color</span>:blue;</span><br><span class="line">&#125;</span><br></pre>

<p>由于div是通过继承性来影响文字颜色的，所以!important无法提升它的权重，权重依然是0。</p>
<p>干不过p标签，因为p标签是实实在在选中了，所以字是蓝色的（以p为准）。</p>
<p><strong>(3)!important不影响就近原则</strong></p>
<p>如果大家都是继承来的，按理说应该按照“就近原则”，那么important能否影响就近原则呢？<br>答案是：不影响。远的，永远是远的。不能给远的写一个important，干掉近的。</p>
<p>为了验证这个问题，我们可以搞两层具有继承性的标签，然后给外层标签加一个!important，最终看看就近原则有没有被打破。举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_2046.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_2046.png"></p>
<p>PS：做网站的时候，!important 尽量不要用。否则会让css写的很乱。</p>
<h2 id="知识回顾"><a href="#知识回顾" class="headerlink" title="知识回顾"></a>知识回顾</h2><blockquote>
<p>我们把以上内容和上一篇文章做一个简单的知识回顾。</p>
</blockquote>
<h3 id="CSS属性"><a href="#CSS属性" class="headerlink" title="CSS属性"></a>CSS属性</h3><blockquote>
<p>css属性，面试的时候会有笔试，笔试没有智能提示的。</p>
</blockquote>
<p>加粗，倾斜，下划线：</p>
<pre class="highlight"><span class="line">font-weight:bold;</span><br><span class="line">font-style:italic;</span><br><span class="line">text-decoration:underline;</span><br></pre>

<p>背景颜色、前景色：</p>
<pre class="highlight"><span class="line">background-color:red;</span><br><span class="line">color:red;</span><br></pre>

<h3 id="class和id的区别"><a href="#class和id的区别" class="headerlink" title="class和id的区别"></a>class和id的区别</h3><p>class用于css的，id用于js的。</p>
<p>1）class页面上可以重复。id页面上唯一，不能重复。<br>2）一个标签可以有多个class，用空格隔开。但是id只能有id。</p>
<h3 id="各种选择器-浏览器兼容性"><a href="#各种选择器-浏览器兼容性" class="headerlink" title="各种选择器(浏览器兼容性)"></a>各种选择器(浏览器兼容性)</h3><p>IE6层面兼容的选择器： 标签选择器、id选择器、类选择器、后代、交集选择器、并集选择器、通配符。如下：</p>
<pre class="highlight"><span class="line">p</span><br><span class="line">#box</span><br><span class="line">.spec</span><br><span class="line">div p</span><br><span class="line">div.spec</span><br><span class="line">div,p</span><br><span class="line">*</span><br></pre>

<p>IE7能够兼容的：儿子选择器、下一个兄弟选择器。如下：</p>
<pre class="highlight"><span class="line">div&gt;p</span><br><span class="line">h3+p</span><br></pre>
<p>IE8能够兼容的：</p>
<pre class="highlight"><span class="line">ul li:first-child</span><br><span class="line">ul li:last-child</span><br></pre>
<h3 id="css两个性质"><a href="#css两个性质" class="headerlink" title="css两个性质"></a>css两个性质</h3><ul>
<li><p>继承性：好的事儿。继承从上到下，哪些能？哪些不能？</p>
</li>
<li><p>层叠性：冲突，多个选择器描述了同一个属性，听谁的？</p>
</li>
</ul>
<p>再看几个题目：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0900.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0900.png"></p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0901.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0901.png"></p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0902.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0902.png"></p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170727_0903.png" class="lozad post-image"src="http://img.smyhvae.com/20170727_0903.png"></p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/05-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7%E5%92%8C%E5%B1%82%E5%8F%A0%E6%80%A7/">
            https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/05-CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7%E5%92%8C%E5%B1%82%E5%8F%A0%E6%80%A7/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/04-CSS%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%9A%E4%BC%AA%E7%B1%BB/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">04-CSS选择器：伪类 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/06-CSS%E7%9B%92%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">06-CSS盒模型详解 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E9%87%8D%E7%82%B9"><span class="toc-text">本文重点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7"><span class="toc-text">CSS的继承性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E5%B1%82%E5%8F%A0%E6%80%A7"><span class="toc-text">CSS的层叠性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">层叠性：计算权重</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%E4%B8%BE%E4%BE%8B"><span class="toc-text">层叠性举例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">举例1：计算权重</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%9D%83%E9%87%8D%E7%9B%B8%E5%90%8C%E6%97%B6"><span class="toc-text">举例2：权重相同时</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B3%EF%BC%9A%E5%85%B7%E6%9C%89%E5%AE%9E%E6%88%98%E6%80%A7%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="toc-text">举例3：具有实战性的例子</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B4%EF%BC%9A%E7%BB%A7%E6%89%BF%E6%80%A7%E9%80%A0%E6%88%90%E7%9A%84%E5%BD%B1%E5%93%8D"><span class="toc-text">举例4：继承性造成的影响</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E6%9D%83%E9%87%8D%E8%AE%A1%E7%AE%97%E7%9A%84%E9%97%AE%E9%A2%98%E5%A4%A7%E6%80%BB%E7%BB%93%EF%BC%88%E9%9D%9E%E5%B8%B8%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">层叠性：权重计算的问题大总结（非常重要）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E5%86%B2%E7%AA%81%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">CSS样式表的冲突的总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%E6%BC%94%E7%A4%BA"><span class="toc-text">题目演示</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE1"><span class="toc-text">题目1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE2"><span class="toc-text">题目2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE3"><span class="toc-text">题目3</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE4"><span class="toc-text">题目4</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%83%E9%87%8D%E9%97%AE%E9%A2%98%E6%B7%B1%E5%85%A5"><span class="toc-text">权重问题深入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E4%B8%80%E4%B8%AA%E6%A0%87%E7%AD%BE%EF%BC%8C%E6%90%BA%E5%B8%A6%E4%BA%86%E5%A4%9A%E4%B8%AA%E7%B1%BB%E5%90%8D%EF%BC%8C%E6%9C%89%E5%86%B2%E7%AA%81%EF%BC%9A"><span class="toc-text">同一个标签，携带了多个类名，有冲突：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#important%E6%A0%87%E8%AE%B0%EF%BC%9A%E4%BC%98%E5%85%88%E7%BA%A7%E6%9C%80%E9%AB%98"><span class="toc-text">!important标记：优先级最高</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE"><span class="toc-text">知识回顾</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#class%E5%92%8Cid%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">class和id的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%84%E7%A7%8D%E9%80%89%E6%8B%A9%E5%99%A8-%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">各种选择器(浏览器兼容性)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E4%B8%A4%E4%B8%AA%E6%80%A7%E8%B4%A8"><span class="toc-text">css两个性质</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E9%87%8D%E7%82%B9"><span class="toc-text">本文重点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E7%BB%A7%E6%89%BF%E6%80%A7"><span class="toc-text">CSS的继承性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#CSS%E7%9A%84%E5%B1%82%E5%8F%A0%E6%80%A7"><span class="toc-text">CSS的层叠性</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">层叠性：计算权重</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%E4%B8%BE%E4%BE%8B"><span class="toc-text">层叠性举例</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B1%EF%BC%9A%E8%AE%A1%E7%AE%97%E6%9D%83%E9%87%8D"><span class="toc-text">举例1：计算权重</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B2%EF%BC%9A%E6%9D%83%E9%87%8D%E7%9B%B8%E5%90%8C%E6%97%B6"><span class="toc-text">举例2：权重相同时</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B3%EF%BC%9A%E5%85%B7%E6%9C%89%E5%AE%9E%E6%88%98%E6%80%A7%E7%9A%84%E4%BE%8B%E5%AD%90"><span class="toc-text">举例3：具有实战性的例子</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BE%E4%BE%8B4%EF%BC%9A%E7%BB%A7%E6%89%BF%E6%80%A7%E9%80%A0%E6%88%90%E7%9A%84%E5%BD%B1%E5%93%8D"><span class="toc-text">举例4：继承性造成的影响</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E5%8F%A0%E6%80%A7%EF%BC%9A%E6%9D%83%E9%87%8D%E8%AE%A1%E7%AE%97%E7%9A%84%E9%97%AE%E9%A2%98%E5%A4%A7%E6%80%BB%E7%BB%93%EF%BC%88%E9%9D%9E%E5%B8%B8%E9%87%8D%E8%A6%81%EF%BC%89"><span class="toc-text">层叠性：权重计算的问题大总结（非常重要）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E6%A0%B7%E5%BC%8F%E8%A1%A8%E7%9A%84%E5%86%B2%E7%AA%81%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">CSS样式表的冲突的总结</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE%E6%BC%94%E7%A4%BA"><span class="toc-text">题目演示</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE1"><span class="toc-text">题目1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE2"><span class="toc-text">题目2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE3"><span class="toc-text">题目3</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%98%E7%9B%AE4"><span class="toc-text">题目4</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9D%83%E9%87%8D%E9%97%AE%E9%A2%98%E6%B7%B1%E5%85%A5"><span class="toc-text">权重问题深入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%8C%E4%B8%80%E4%B8%AA%E6%A0%87%E7%AD%BE%EF%BC%8C%E6%90%BA%E5%B8%A6%E4%BA%86%E5%A4%9A%E4%B8%AA%E7%B1%BB%E5%90%8D%EF%BC%8C%E6%9C%89%E5%86%B2%E7%AA%81%EF%BC%9A"><span class="toc-text">同一个标签，携带了多个类名，有冲突：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#important%E6%A0%87%E8%AE%B0%EF%BC%9A%E4%BC%98%E5%85%88%E7%BA%A7%E6%9C%80%E9%AB%98"><span class="toc-text">!important标记：优先级最高</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE"><span class="toc-text">知识回顾</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS%E5%B1%9E%E6%80%A7"><span class="toc-text">CSS属性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#class%E5%92%8Cid%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">class和id的区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%84%E7%A7%8D%E9%80%89%E6%8B%A9%E5%99%A8-%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%BC%E5%AE%B9%E6%80%A7"><span class="toc-text">各种选择器(浏览器兼容性)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css%E4%B8%A4%E4%B8%AA%E6%80%A7%E8%B4%A8"><span class="toc-text">css两个性质</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
